/**
 * 获取 URL Search 参数
 * @param {boolean} noDecode 不解码
 * @return
 */
export const getSearchParams = (key, url?) => {
  const hashSearch = window.location.hash.split("?")[1];
  const str = url || window.location.search || `?${hashSearch}`;
  if (str.includes("?") && str.includes(key)) {
    const arr: any = str
      .split("?")
      .filter((x) => x.includes(key))
      .join()
      .split("&");
    const value = arr
      .find((_) => {
        return _.includes(key);
      })
      .split("=")[1];
    return value;
  } else {
    return "";
  }
};

/**
 * 获取URL参数
 * @param {String} url 目标URL地址
 * @return {Object} 参数对象
 */
export function getQueryObject(url) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    `{"${decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"')}"}`
  );
}
